//
//  TabbarViewController.m
//  huashiedu
//
//  Created by oceans on 2018/8/24.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "TabbarViewController.h"
#import "HomeViewController.h"
#import "DiscoveryViewController.h"
#import "MessageViewController.h"
#import "MineViewController.h"
#import "LoginViewController.h"
#import "NewDynamicsViewController.h"

@interface TabbarViewController () <UITabBarControllerDelegate>

@end

@implementation TabbarViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    
    self.delegate = self;
    self.tabBar.tintColor = COLOR(51, 51, 51, 1);
    self.tabBar.barTintColor = [UIColor whiteColor];
    
    HomeViewController *home = [[HomeViewController alloc] init];
    home.tabBarItem.title = @"首页";
    home.tabBarItem.image = [UIImage imageNamed:@"tabbar_0"];
    home.tabBarItem.tag = 0;
    
    NewDynamicsViewController *discovery = [[NewDynamicsViewController alloc] init];
    discovery.tabBarItem.title = @"发现";
    discovery.tabBarItem.image = [UIImage imageNamed:@"tabbar_1"];
    discovery.tabBarItem.tag = 1;
    
    MessageViewController *message = [[MessageViewController alloc] init];
    message.tabBarItem.title = @"消息";
    message.tabBarItem.image = [UIImage imageNamed:@"tabbar_2"];
    message.tabBarItem.tag = 2;
    
    MineViewController *mine = [[MineViewController alloc] init];
    mine.tabBarItem.title = @"我的";
    mine.tabBarItem.image = [UIImage imageNamed:@"tabbar_3"];
    mine.tabBarItem.tag = 3;
    
    self.viewControllers = @[home, discovery, message, mine];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
    if (viewController.tabBarItem.tag == 2) {
        
        if ([UserModel shared].hasLogin == NO) {
            UINavigationController *controller = [LoginViewController loginViewController];
            [self.navigationController presentViewController:controller animated:YES completion:nil];
            return NO;
        }
    }
    [viewController performSelector:@selector(setupNavigation) withObject:nil];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
